package cn.itcast.utils;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * 获取接口代理对象
 */
public class MapperUtil {
    private static  Object mapper = null;
    private MapperUtil(){}

    public  static Object getMapper(Class<?> clzz){

        try {
            //加载核心配置文件
            InputStream is = Resources.getResourceAsStream("sqlConfig.xml");
            //构建factory
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
            //获取sqlssion
            SqlSession sqlSession = sessionFactory.openSession(true);

             mapper = sqlSession.getMapper(clzz);


        } catch (IOException e) {
            e.printStackTrace();
        }
        return mapper;
    }

}
